import Base from 'models/base';
import i18n from 'i18n';

import SystemUtil from 'utils/systemUtil';

const activityNameObj = {
  "1": "砍价",
  "2": "拼团",
  "3": "一元拍卖"
};

export default class RefundOrder extends Base {
  constructor(order) {
    super();

    let fields = ['orderNoSub', 'branchId', 'partnerId', 'branchName', 'partnerName', 'buyerName', 'buyerAddress', 'buyerPhone'];
    this.initObject(order, fields);

    let orderId = order.orderNoMain;
    let status = order.status;
    this.orderId = orderId;
    this.status = status;
    this.id = `${orderId}_${status}`;
    this.payPrice = order.shouldPayPrice;
    this.orderRefuseApplyId = order.orderRefuseApplyId;
    
    let reason = order.applyRemark;
    if (SystemUtil.isEmpty(reason)) {
      reason = '';
    } else {
      reason = _.escape(reason);
      reason = reason.replace(/ /g,'&nbsp;').replace(/\n/,'<br>');
    }
    this.reasonHtml = reason;
    this.reason = order.applyRemark;

    let remark = order.auditRemark;
    if (SystemUtil.isEmpty(remark)) {
      remark = '';
    } else {
      remark = _.escape(remark);
      remark = remark.replace(/ /g,'&nbsp;').replace(/\n/,'<br>');
    }
    this.remarkHtml = remark;
    this.remark = order.auditRemark;

    let refusePrice = order.refusePrice;
    if (SystemUtil.isEmpty(refusePrice)) {
      refusePrice = '';
    } else {
      refusePrice = _.round(refusePrice / 100, 2);
    }
    this.refusePrice = refusePrice;

    let preferentialPrice = order.preferentialPrice;
    if (SystemUtil.isEmpty(preferentialPrice)) {
      preferentialPrice = 0;
    }
    this.preferentialPrice = preferentialPrice;

    let approvalName = order.auditUsername;
    if (SystemUtil.isEmpty(approvalName)) {
      approvalName = '';
    }
    this.approvalName = approvalName;

    let statusStr;
    if (status === 2 && approvalName !== '') {
      statusStr = '已同意,未到账';
    } else {
      statusStr = i18n.getMsg(`orderMgr.status.${status}`);
    }
    this.statusStr = statusStr;

    this.setDatetimeData(order, ['createTime', 'payTime', 'refuseTime']);

    let type = order.sourceSub;
    this.type = type;
    let activityTypeId = order.activityTypeId;
    this.activityTypeId = activityTypeId;

    if (type === 'YDDD') {
      let activityName = activityNameObj[activityTypeId];
      if (activityName === undefined) {
        this.name = `${order.goodsName} 预付款`;
      } else {
        this.name = `${activityName}-${order.goodsName} 预付款`;
      }
      return;
    }
    if (type === 'NOGOODS') {
      this.name = '门店支付';
      return;
    }
    this.defId = order.goodsDefId;
    this.modelId = order.goodsModelId;
    this.name = order.goodsName;
    this.modelName = order.goodsModelName;
    this.price = order.goodsModelSalePrice;
    this.sourcePrice = order.goodsModelSourcePrice;
    this.count = order.buyCount;
    this.buyMethod = order.buyMethod;
  }
}
